package com.suzhiliang.springbootlesson.securityOAuth2;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Copyright (C), 2015-2019, XXX有限公司
 *
 * @ClassName: CustomAuthenticationEntryPoint
 * @Author: xmm
 * @Date: 2019/7/23 10:06
 * @Description: 自定义401错误码内容
 *                配置如果没有权限,访问接口时我们返回的错误码以及错误内容
 * @Version 1.0
 */
@Component
public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint {

    private static Logger logger = LoggerFactory.getLogger(CustomAuthenticationEntryPoint.class);

    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        logger.info("进入401错误处理类");
        httpServletResponse.sendError(HttpServletResponse.SC_ACCEPTED,"Access Denied");
    }
}
